<template>
  <el-dialog
    :title="!dataForm.schoolId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地区id(省份id)" prop="shenId">
      <el-input v-model="dataForm.shenId" placeholder="地区id(省份id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(市id)" prop="shiId">
      <el-input v-model="dataForm.shiId" placeholder="地区id(市id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(县id)" prop="xianId">
      <el-input v-model="dataForm.xianId" placeholder="地区id(县id)"></el-input>
    </el-form-item>
    <el-form-item label="学校名字" prop="schoolName">
      <el-input v-model="dataForm.schoolName" placeholder="学校名字"></el-input>
    </el-form-item>
    <el-form-item label="学校地址  省市县" prop="address">
      <el-input v-model="dataForm.address" placeholder="学校地址  省市县"></el-input>
    </el-form-item>
    <el-form-item label="学校性质，如：公立学校" prop="nature">
      <el-input v-model="dataForm.nature" placeholder="学校性质，如：公立学校"></el-input>
    </el-form-item>
    <el-form-item label="坐标位置-经度" prop="locationX">
      <el-input v-model="dataForm.locationX" placeholder="坐标位置-经度"></el-input>
    </el-form-item>
    <el-form-item label="坐标位置-纬度" prop="locationY">
      <el-input v-model="dataForm.locationY" placeholder="坐标位置-纬度"></el-input>
    </el-form-item>
    <el-form-item label="类型 1-幼儿园   2-小学 3-初中 4-高中" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型 1-幼儿园   2-小学 3-初中 4-高中"></el-input>
    </el-form-item>
    <el-form-item label="缩略图" prop="image">
      <el-input v-model="dataForm.image" placeholder="缩略图"></el-input>
    </el-form-item>
    <el-form-item label="介绍" prop="schoolDesc">
      <el-input v-model="dataForm.schoolDesc" placeholder="介绍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          schoolId: 0,
          shenId: '',
          shiId: '',
          xianId: '',
          schoolName: '',
          address: '',
          nature: '',
          locationX: '',
          locationY: '',
          type: '',
          image: '',
          schoolDesc: ''
        },
        dataRule: {
          shenId: [
            { required: true, message: '地区id(省份id)不能为空', trigger: 'blur' }
          ],
          shiId: [
            { required: true, message: '地区id(市id)不能为空', trigger: 'blur' }
          ],
          xianId: [
            { required: true, message: '地区id(县id)不能为空', trigger: 'blur' }
          ],
          schoolName: [
            { required: true, message: '学校名字不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '学校地址  省市县不能为空', trigger: 'blur' }
          ],
          nature: [
            { required: true, message: '学校性质，如：公立学校不能为空', trigger: 'blur' }
          ],
          locationX: [
            { required: true, message: '坐标位置-经度不能为空', trigger: 'blur' }
          ],
          locationY: [
            { required: true, message: '坐标位置-纬度不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型 1-幼儿园   2-小学 3-初中 4-高中不能为空', trigger: 'blur' }
          ],
          image: [
            { required: true, message: '缩略图不能为空', trigger: 'blur' }
          ],
          schoolDesc: [
            { required: true, message: '介绍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.schoolId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.schoolId) {
            this.$http({
              url: this.$http.adornUrl(`/house/school/info/${this.dataForm.schoolId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shenId = data.school.shenId
                this.dataForm.shiId = data.school.shiId
                this.dataForm.xianId = data.school.xianId
                this.dataForm.schoolName = data.school.schoolName
                this.dataForm.address = data.school.address
                this.dataForm.nature = data.school.nature
                this.dataForm.locationX = data.school.locationX
                this.dataForm.locationY = data.school.locationY
                this.dataForm.type = data.school.type
                this.dataForm.image = data.school.image
                this.dataForm.schoolDesc = data.school.schoolDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/school/${!this.dataForm.schoolId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'schoolId': this.dataForm.schoolId || undefined,
                'shenId': this.dataForm.shenId,
                'shiId': this.dataForm.shiId,
                'xianId': this.dataForm.xianId,
                'schoolName': this.dataForm.schoolName,
                'address': this.dataForm.address,
                'nature': this.dataForm.nature,
                'locationX': this.dataForm.locationX,
                'locationY': this.dataForm.locationY,
                'type': this.dataForm.type,
                'image': this.dataForm.image,
                'schoolDesc': this.dataForm.schoolDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
